<template>
  <!-- !租客账单 -->
  <div class="room-bill" style="height: 100%">
    <a-card size="small" style="height: 100%" v-if="hasOwner">
      <!-- 租客显示 -->
      <template v-if="currentOwner.id">
        <a-row type="flex" align="top" justify="space-between">
          <a-col>
            <a-dropdown v-if="roomOwnerList.length > 1" overlayClassName="hide-scroller-bar" :overlayStyle="{
              height: '500px',
              overflow: 'hidden auto',
              borderRadius: '10px'
            }">
              <div class="owner-name">
                <a-space>
                  <span>{{ currentOwner.owner_name }}</span>
                  <a-tag v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
                  <a-tag v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
                  <DownOutlined />
                </a-space>
              </div>
              <template #overlay>
                <a-menu @click="onRoomOwnerChange" style="border: 1px solid #f0f0f0">
                  <a-menu-item v-for="item of roomOwnerList" :key="item.id">
                    <span>{{ item.owner_name }}</span>
                    <a-tag style="margin-left: 10px;" v-if="item.status == 0" color="#87d068">{{ item.owner_type }}</a-tag>
                    <a-tag style="margin-left: 10px;" v-else color="#777777">{{ item.owner_type }}</a-tag>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
            <div class="owner-name" v-else>
              <span>{{ currentOwner.owner_name }}</span>
              <a-tag style="margin-left: 10px;" v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
              <a-tag style="margin-left: 10px;" v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
            </div>
          </a-col>
          <a-col>
            <a-button type="link" @click="checkOwnerDetail">
              租客详情
            </a-button>
          </a-col>
        </a-row>
        <a-row :gutter="[8, 12]" class="mt-10">
          <a-col :span="6">
            <a-card
              size="small"
              class="statistic"
              :bodyStyle="{ padding: '8px' }"
            >
              <a-statistic
                class="cont"
                title="逾期账单数"
                :value="statisData.overdueBillCount"
              />
              <div class="action">
                <a-button
                  type="link"
                  :style="{
                    height: '24px',
                    lineHeight: '24px',
                    padding: '0px',
                    fontSize: '12px',
                  }"
                  @click="noticeVisible = true"
                >
                  一键发送通知单
                </a-button>
              </div>
            </a-card>
          </a-col>
          <a-col :span="6">
            <a-card
              size="small"
              class="statistic"
              :bodyStyle="{ padding: '8px' }"
            >
              <a-statistic
                class="cont"
                title="累计流水"
                :value="statisData.streamTotal"
              />
              <div class="action">
                <a-space>
                  <span>未匹配流水</span>
                  <span>¥{{ statisData.noMatchStream }}</span>
                </a-space>
              </div>
            </a-card>
          </a-col>
          <a-col :span="6">
            <a-card
              size="small"
              class="statistic"
              :bodyStyle="{ padding: '8px' }"
            >
              <a-statistic
                class="cont"
                title="需收金额"
                :value="statisData.actualRetreat"
              />
              <div class="action">
                <a-space>
                  <span>应收金额</span>
                  <span>¥{{ statisData.receivablePayment }}</span>
                </a-space>
              </div>
            </a-card>
          </a-col>
          <a-col :span="6">
            <a-card
              size="small"
              class="statistic"
              :bodyStyle="{ padding: '8px' }"
            >
              <a-statistic
                class="cont"
                title="待开发票"
                :value="statisData.unInvoiceAmount"
              />
              <div class="action">
                <a-space>
                  <span>已开票</span>
                  <span>¥{{ statisData.invoiceAmount }}</span>
                </a-space>
              </div>
            </a-card>
          </a-col>

          <a-col :span="24" v-if="monthList.length">
            <a-card size="small" title="账单记录">
              <a-tabs type="card">
                <a-tab-pane
                  v-for="month of monthList"
                  :key="month.sort"
                  :tab="month.month"
                >
                  <a-table
                    :dataSource="month.billList"
                    :columns="BILL_COLUMNS"
                    :pagination="false"
                    rowKey="id"
                    :loading="billLoading"
                    size="small"
                    bordered
                  >
                    <template #title>
                      <a-space>
                        <span>应收总计：</span>
                        <span> {{ month.receiveTotal }} </span>
                      </a-space>
                    </template>

                    <template #bodyCell="{ text, record, index, column }">
                      <template v-if="column.key === 'bill_number'">
                        <a-button type="link" @click="onBillClick(record)">{{
                          text
                        }}</a-button>
                      </template>
                    </template>
                  </a-table>
                </a-tab-pane>
              </a-tabs>
            </a-card>
          </a-col>
        </a-row>
      </template>
    </a-card>
    <div class="no-owner" v-else>
      <a-empty description="当前房源暂无账单记录" />
    </div>

    <!-- 租客详情 -->
    <OwnerDetailDrawer
      :visible="ownerDetailVisible"
      :id="currentOwner.id"
      @cancel="onOwnerDetailClose"
    />

    <!-- 账单详情 -->
    <BillDetailDrawer
      :visible="billDetailVisible"
      :id="currentBill.id"
      @cancel="onBillDetailClose"
    />

    <!-- 缴费通知 -->
    <AddPayNotice
      :visible="noticeVisible"
      :ids="statisData.overdueBillIds"
      @closeDrawer="noticeVisible = false"
      @success="onNoticeSuccess"
    />
  </div>
</template>
<script>
import { defineComponent, reactive, toRefs } from "vue";
import {
  PlusOutlined,
  DownOutlined,
  PlusCircleOutlined,
} from "@ant-design/icons-vue";
import OwnerDetailDrawer from "../../../views/village/OwnerManage/modules/OwnerDetailDrawer.vue";
import BillDetailDrawer from "../../../views/bill/BillDetail/modules/BillDetailDrawer.vue";
import AddPayNotice from "../../../views/bill/AllBills/AddNotice.vue";
import request from "../../../common/utils/request";
import { useRouter } from "vue-router";

export default defineComponent({
  name: "RoomBillRecord",
  props: {
    detail: {
      type: Object,
      default: () => ({}),
    },
  },
  components: {
    PlusOutlined,
    DownOutlined,
    PlusCircleOutlined,
    OwnerDetailDrawer,
    BillDetailDrawer,
    AddPayNotice,
  },
  setup(props, { emit }) {
    const ROUTER = useRouter();

    const state = reactive({
      roomOwnerList: [],
      currentOwner: {},
      hasOwner: true,
      ownerDetailVisible: false,

      statisData: {},
      monthList: [],
      totalInfo: {},
      billDetailVisible: false,
      currentBill: {},

      noticeVisible: false,
    });

    const onBillClick = (record) => {
      state.currentBill = record;
      state.billDetailVisible = true;
    };

    const onNoticeSuccess = () => {
      state.noticeVisible = false;
    };

    const onBillDetailClose = () => {
      state.billDetailVisible = false;
      getRoomOwnerList();
    };

    /** 查看租客详情 */
    const checkOwnerDetail = () => {
      state.ownerDetailVisible = true;
    };
    const onOwnerDetailClose = () => {
      state.ownerDetailVisible = false;
      getRoomOwnerList();
    };

    /** 选中租客 */
    const onRoomOwnerChange = (e) => {
      const { key } = e;
      state.currentOwner = state.roomOwnerList.find((it) => it.id == key);
      getBillInfo();
      getBillList();
    };

    /** 获取租客列表 */
    const getRoomOwnerList = () => {
      request
        .get("/village", "/project/room/getOwnerList", {
          build_id: props.detail.build_id,
          room_id: props.detail.id,
        })
        .then((res) => {
          state.roomOwnerList = [];
          if (res.list && res.list.length > 0) {
            state.hasOwner = true;
            state.roomOwnerList = res.list;
            let index = -1;
            if (state.currentOwner.id) {
              index = state.roomOwnerList.findIndex(
                (it) => it.id == state.currentOwner.id
              );
            }
            if (index == -1) {
              index = 0;
            }
            onRoomOwnerChange({ key: res.list[index].id });
          } else {
            state.hasOwner = false;
          }
        });
    };

    const getBillList = () => {
      state.billLoading = true;
      request
        .get("/org", "/owner/ownerFinanceList", {
          owner_id: state.currentOwner.id,
          room_id: props.detail.id,
        })
        .then((res) => {
          state.totalInfo = {
            allActualAmount: res.allActualAmount,
            allBadAmount: res.allBadAmount,
            allReceiveAmount: res.allReceiveAmount,
          };
          state.monthList = res.newList;
        })
        .finally(() => {
          state.billLoading = false;
        });
    };

    const getBillInfo = () => {
      request
        .get("/org", "/owner/ownerBillStatic", {
          owner_id: state.currentOwner.id,
          room_id: props.detail.id,
        })
        .then((res) => {
          state.statisData = res.returnData;
        });
    };

    const init = () => {
      getRoomOwnerList();
    };

    init();

    return {
      BILL_COLUMNS,
      ...toRefs(state),
      onRoomOwnerChange,
      checkOwnerDetail,
      onOwnerDetailClose,
      onBillDetailClose,
      onNoticeSuccess,
      onBillClick,
    };
  },
});

const BILL_COLUMNS = [
  {
    title: "账单编号",
    dataIndex: "bill_number",
    key: "bill_number",
    align: "center",
  },
  {
    title: "应收时间",
    dataIndex: "receivable_payment_date",
    align: "center",
  },
  {
    title: "费用类型",
    dataIndex: "cost_type_txt",
    align: "center",
  },
  {
    title: "账单状态",
    dataIndex: "timeout_status_txt",
    align: "center",
  },
  {
    title: "逾期状态",
    dataIndex: "settle_status_txt",
    align: "center",
  },
  {
    title: "应收金额",
    dataIndex: "receivable_payment",
    align: "receivable_payment",
  },
];
</script>

<style lang="less" scoped>
.no-owner {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.owner-name {
  font-size: 15px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
}

.statistic {
  .cont {
    height: 80px;
  }
  .action {
    line-height: 24px;
    border-top: 1px solid #f1f1f1;
    padding-top: 8px;
    color: rgba(0, 0, 0, 0.4);
    font-size: 12px;
  }
}
</style>
